<template>
  <div class="main-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '250px'" class="sidebar">
        <div class="logo" @click="toggleCollapse">
          <el-icon size="30" color="#409EFF"><WaterIcon /></el-icon>
          <span v-if="!isCollapse" class="logo-text">水云鉴</span>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          @select="handleMenuSelect"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span v-if="!isCollapse">首页</span>
          </el-menu-item>
          
          <el-menu-item index="/water-quality/device">
            <el-icon><Monitor /></el-icon>
            <span v-if="!isCollapse">设备管理</span>
          </el-menu-item>
          
          <el-menu-item index="/water-quality/visualization">
            <el-icon><DataLine /></el-icon>
            <span v-if="!isCollapse">可视化界面</span>
          </el-menu-item>
          
          <el-menu-item index="/water-quality/system">
            <el-icon><Setting /></el-icon>
            <span v-if="!isCollapse">系统管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航栏 -->
        <el-header class="header">
          <div class="header-left">
            <el-button class="menu-toggle" @click="toggleCollapse">
              <el-icon><MenuIcon /></el-icon>
            </el-button>
            <el-breadcrumb separator="/" class="breadcrumb">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item v-if="currentPageTitle">{{ currentPageTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-info">
                <el-icon><User /></el-icon>
                <span v-if="!isCollapse">管理员</span>
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="settings">系统设置</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 主内容 -->
        <el-main class="main-content">
          <router-view :key="route.fullPath" />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Setting as WaterIcon, 
  House, 
  Monitor, 
  DataLine, 
  Setting, 
  User, 
  ArrowDown,
  Menu as MenuIcon
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 响应式状态
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = ref('/')

// 当前页面标题
const currentPageTitle = computed(() => {
  return route.meta?.title || ''
})

// 监听路由变化，更新激活菜单
watch(() => route.path, (newPath) => {
  activeMenu.value = newPath
}, { immediate: true })

// 菜单折叠切换
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 菜单选择处理
const handleMenuSelect = (index) => {
  console.log('菜单选择:', index)
  console.log('当前路由:', route.path)
  console.log('目标路由:', index)
  
  // 使用replace而不是push来避免历史记录问题
  router.replace(index)
}

// 用户下拉菜单处理
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人中心功能')
      break
    case 'settings':
      ElMessage.info('系统设置功能')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 登出处理
const handleLogout = async () => {
  try {
    // 这里可以调用登出API
    // await logout()
    
    // 清除本地存储
    localStorage.removeItem('userInfo')
    sessionStorage.removeItem('userInfo')
    localStorage.removeItem('token')
    
    ElMessage.success('已退出登录')
    
    // 跳转到登录页
    router.push('/login')
  } catch (error) {
    console.error('登出失败:', error)
    ElMessage.error('登出失败')
  }
}
</script>

<style scoped>
.main-layout {
  height: 100vh;
  background: #f5f7fa;
  font-family: 'Helvetica Neue', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

.sidebar {
  background: #ffffff;
  color: #333333;
  box-shadow: 2px 0 12px rgba(0, 0, 0, 0.05);
  border-right: 1px solid #ebeef5;
  transition: width 0.3s ease;
  overflow: hidden;
}

.logo {
  display: flex;
  align-items: center;
  padding: 25px 20px;
  border-bottom: 1px solid #ebeef5;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logo:hover {
  background-color: #f5f7fa;
}

.logo-text {
  margin-left: 12px;
  font-size: 22px;
  font-weight: 600;
  color: #333333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-menu {
  border: none;
  background: transparent;
  padding: 10px 0;
}

.sidebar-menu .el-menu-item {
  color: #606266;
  margin: 2px 10px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #ecf5ff;
  color: #409eff;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #ecf5ff;
  color: #409eff;
}

.header {
  background: #ffffff;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.02);
}

.header-left {
  display: flex;
  align-items: center;
  flex: 1;
}

.menu-toggle {
  margin-right: 15px;
  padding: 8px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.menu-toggle:hover {
  background-color: #f5f7fa;
}

.breadcrumb {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 15px;
  border-radius: 20px;
  transition: all 0.3s ease;
  background-color: #f5f7fa;
  color: #606266;
  white-space: nowrap;
}

.user-info:hover {
  background-color: #ecf5ff;
  color: #409eff;
}

.main-content {
  padding: 20px;
  background: #f5f7fa;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 64px !important;
  }
  
  .logo-text {
    display: none;
  }
  
  .header {
    padding: 0 15px;
  }
  
  .menu-toggle {
    margin-right: 10px;
  }
  
  .user-info span:not(.el-icon) {
    display: none;
  }
  
  .user-info {
    padding: 8px 10px;
  }
  
  .main-content {
    padding: 15px;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 0 10px;
  }
  
  .menu-toggle {
    padding: 6px;
  }
}
</style>